package com.whut.applicationRunner;

import com.hankcs.hanlp.mining.word2vec.DocVectorModel;
import com.hankcs.hanlp.mining.word2vec.WordVectorModel;
import com.hankcs.hanlp.suggest.Suggester;
import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.springframework.boot.ApplicationArguments;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

//@Component
public class ApplicationRunner implements org.springframework.boot.ApplicationRunner {
    public static String[] userDictionary = null;
    public static Suggester suggester = new Suggester();
    public static DocVectorModel docVectorModel = null;

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        String commonPath = "E:/IdeaWorkSpace/yirui_sc/src/main/resources/data/"; //本地路径
//        String commonPath = "D:/whutServer/apache-tomcat-8.5.41/webapps/ROOT/WEB-INF/classes/data/"; //正式路径
        String commonPath = "/hfhk/check/server/app/BOOT-INF/classes/data/"; //linux路径
        long str = System.currentTimeMillis();
        try {
            System.out.println("开始读取词向量库");
            //获取pd词典
            File dictionaryPath = new File(commonPath + "dictionary/pdDictionary.txt");
            String dictionary = readTxtFile(dictionaryPath.toString());
            userDictionary = dictionary.split("\\n");
            //读取词向量库
            File wordVectorModelPath = new File(commonPath + "word-vector/sgns.baidubaike.txt");
            docVectorModel = new DocVectorModel(new WordVectorModel(wordVectorModelPath.toString()));
            if (userDictionary != null && docVectorModel != null) {
                for (int i = 0; i < userDictionary.length; i++) {
                    docVectorModel.addDocument(i, userDictionary[i]);
                }
                long end = System.currentTimeMillis();
                System.out.print("读取词向量库耗时:" + (end - str));
            } else {
                System.out.print("读取词向量库失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String readTxtFile(String filePath) {
        String text = "";
        try {
            String encoding = "UTF-8";
            File file = new File(filePath);
            if (file.isFile() && file.exists()) { //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    text += lineTxt + "\n";
                }
                read.close();
                return text;
            } else {
                System.out.println("找不到指定的文件");
                return null;
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return text;
    }
}
